<script setup>
  import { User, Lock } from '@element-plus/icons-vue'
  import { userLoginService } from '@/api/user.js'
  import { ref } from 'vue'
  import { useUserStore } from '@/stores'
  import { useRouter } from 'vue-router'
  const formModel = ref({
    name: '',
    password: '',
    identity: ''
  })
  const userStore = useUserStore()
  const router = useRouter()
  const login = async () => {
    const res = await userLoginService(formModel.value)
    userStore.setToken(res.data.token)
    if(formModel.value.identity==0){
      userStore.setUser(res.data.student)
    }else{
      userStore.setUser(res.data.teacher)
    }
    

    ElMessage.success('登录成功')
    
    router.push("/")
  }
  </script>
  
  <template>
    <el-row class="login-page">
      <el-col :span="12" class="bg"></el-col>
      <el-col :span="6" :offset="3" class="form">
       
        <el-form ref="form" size="large" autocomplete="off" :model="formModel">
          <el-form-item>
            <h1>登录</h1>
          </el-form-item>
          <el-form-item>
            <el-radio-group v-model="formModel.identity">
              <el-radio label="0">学生</el-radio>
              <el-radio label="1">老师</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="formModel.name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              name="password"
              :prefix-icon="Lock"
              type="password"
              placeholder="请输入密码"
              v-model="formModel.password"
            ></el-input>
          </el-form-item>
          <el-form-item class="flex">
            <div class="flex">
              <el-checkbox>记住我</el-checkbox>
              <el-link type="primary" :underline="false">忘记密码？</el-link>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button class="button" type="primary" auto-insert-space
            @click="login"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </template>
  
  <style lang="scss" scoped>
  .login-page {
    height: 100vh;
    background-color: #fff;
    .bg {
      background:
        url('@/assets/bj.png') no-repeat center / cover;
      border-radius: 0 20px 20px 0;
    }
    .form {
      display: flex;
      flex-direction: column;
      justify-content: center;
      user-select: none;
      .title {
        margin: 0 auto;
      }
      .button {
        width: 100%;
      }
      .flex {
        width: 100%;
        display: flex;
        justify-content: space-between;
      }
    }
  }
  </style>